﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Enterprisess;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.PigBaby
{
    public class AddPigHander : IRequestHandler<AddPigCommand, Result>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        private readonly IMapper mapper;
        
        private readonly IBaseRepository<PigBabys> pigBabysrepository;

        public AddPigHander(IMapper mapper, IBaseRepository<PigBabys> pigBabysrepository)
        {
            this.mapper = mapper;
            this.pigBabysrepository = pigBabysrepository;
        }
        /// <summary>
        /// 后裔添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddPigCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            PigBabys entity = mapper.Map<PigBabys>(request);
            var res = await pigBabysrepository.Add(entity);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
            }
            result.Msg = "添加成功";
            return result;

        }
    }
}
